home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AOL File Library: 2,801 to 2,900
/
aol-file-protocol-4400-2801-to-2900.zip
/
AOLDLs
/
C++ Files Library
/
LFolderWatcher (PP)
/
LFolderWatcher.sit
/
LFolderWatcher
/
LFolderWatcher.cp
< prev
next >
Wrap
Text File
|
1995-08-27
|
5KB
|
184 lines
// ===========================================================================
// LFolderWatcher.cp ⌐ 1995, âric Forget. All rights reserved.
// ===========================================================================
//
// ************************************************************************
// * *
// * Before using this code you should read the "License Agreement" *
// * document and agree with it. *
// * *
// ************************************************************************
//
// LFolderWatcher, as its name tell, watch a folder for any modification in
// it. LFolderWatcher is a subclass of LSortedList.
//
// ---------------------------------------------------------------------------
//
// Instruction Notes:
// -----------------
//
// 1) Create a LFolderWatcher object: inFileCreator and inFileType are optional;
//
// 2) Do: if(myWatcher->Update()) {
//
// // The contains of the folder has changed!
//
// LListIterator iterator(*myWatcher, iterate_FromStart);
// FSSpec theSpec;
// while (iterator.Next(&theSpec)) {
//
// // Do what you need with theSpec
// }
// }
//
// ---------------------------------------------------------------------------
//
// Usage Notes:
// -----------
//
// If you specify inFileCreator or InFileType with "fileType_Default"
// LFolderWatcher will interpret it as any type.
//
// ---------------------------------------------------------------------------
#include "LFolderWatcher.h"
#include "LFSSpecComparator.h"
// ---------------------------------------------------------------------------
// Ñ Constants
// ---------------------------------------------------------------------------
const char Mask_IsDirectory = 0x0010;
const char kIsAlias = 0x8000;
// ---------------------------------------------------------------------------
// Ñ LFolderWatcher
// ---------------------------------------------------------------------------
LFolderWatcher::LFolderWatcher(
Int32 inDirID,
Int16 inVRefNum,
OSType inFileCreator,
OSType inFileType)
: LSortedList(sizeof(FSSpec))
{
mDirID = inDirID;
mVRefNum = inVRefNum;
mFileType = inFileType;
mFileCreator = inFileCreator;
mLastModificationDate = 0;
SetComparator(new LFSSpecComparator);
}
// ---------------------------------------------------------------------------
// Ñ ~LFolderWatcher
// ---------------------------------------------------------------------------
LFolderWatcher::~LFolderWatcher()
{
}
// ---------------------------------------------------------------------------
// Ñ Update
// ---------------------------------------------------------------------------
Boolean
LFolderWatcher::Update()
{
OSErr err;
CInfoPBRec cBlock;
Str63 stringName;
Boolean rval = false;
cBlock.dirInfo.ioCompletion = nil;
cBlock.dirInfo.ioVRefNum = mVRefNum;
cBlock.dirInfo.ioDrDirID = mDirID;
cBlock.dirInfo.ioNamePtr = stringName;
cBlock.dirInfo.ioFDirIndex = -1;
err = ::PBGetCatInfoSync(&cBlock);
if((err == noErr) && (cBlock.dirInfo.ioDrMdDat != mLastModificationDate)) {
// The contains of the folder has changed!
Reset();
mLastModificationDate = cBlock.dirInfo.ioDrMdDat;
rval = true;
}
return rval;
}
// ---------------------------------------------------------------------------
// Ñ Reset
// ---------------------------------------------------------------------------
void
LFolderWatcher::Reset()
{
OSErr err;
CInfoPBRec cBlock;
Str255 fileName;
FSSpec fileSpec;
FInfo fndrInfo;
cBlock.hFileInfo.ioCompletion = nil;
cBlock.hFileInfo.ioNamePtr = fileName;
cBlock.hFileInfo.ioFDirIndex = 1;
RemoveItemsAt(GetCount(), 1);
do {
cBlock.hFileInfo.ioVRefNum = mVRefNum;
cBlock.hFileInfo.ioDirID = mDirID;
err = ::PBGetCatInfoSync(&cBlock);
cBlock.hFileInfo.ioFDirIndex++;
if(err == noErr) {
if(!(cBlock.dirInfo.ioFlAttrib & Mask_IsDirectory)) {
if((err = ::FSMakeFSSpec(mVRefNum, mDirID, fileName, &fileSpec)) == noErr) {
Boolean targetIsFolder, wasAliased;
fndrInfo = cBlock.hFileInfo.ioFlFndrInfo;
if((err = ::ResolveAliasFile(&fileSpec, true, &targetIsFolder, &wasAliased)) != noErr) {
if(wasAliased) {
err = ::FSpGetFInfo(&fileSpec, &fndrInfo);
}
}
if((err == noErr) &&
(mFileCreator == fileType_Default || fndrInfo.fdCreator == mFileCreator) &&
(mFileType == fileType_Default || fndrInfo.fdType == mFileType)) {
AddItem(&fileSpec);
}
}
}
err = noErr;
}
} while(err == noErr);
}